package com.ocom.common.utils.zkzl;

import com.ocom.common.utils.StringUtil;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 加密工具类。业务代码需要加密解密功能，统一调用此工具类。不用暴露MD5和AESHelper
 * @author chenzy
 * @since 2019.12.17
 */
public class SecretUtil {
    private SecretUtil() {

    }
    /**
     * md5加密
     */
    public static String encryptByMD5(SecretRule secretRule, String data) {
        return MD5Secret.md5(secretRule.getMd5() + data);
    }

    /**
     * aes加密
     */
    public static String encryptByAES(SecretRule secretRule, String data) {
        if (StringUtil.isBlank(secretRule.getAes())) {
            return data;
        }
        return AESSecret.encrypt(data, secretRule.getAes());
    }

    /**
     * aes解密
     */
    public static String decryptByAES(SecretRule secretRule, String data) {
        if (StringUtil.isBlank(secretRule.getAes())) {
            return data;
        }
        try {
            return AESSecret.decrypt(data, secretRule.getAes());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Map<String, Object> doGet(String url, String sid, String sign) {

        Map<String, Object> map = new HashMap<>();
        CloseableHttpClient httpClient = HttpClients.createDefault();

        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(5000) // 连接超时
                .setConnectionRequestTimeout(5000)// 请求超时
                .setSocketTimeout(5000).setRedirectsEnabled(true) // 允许自动重定向
                .build();

        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("X-Sid", sid);
        httpGet.setHeader("X-Sign", sign);
        httpGet.setConfig(requestConfig);
        int code = 500;
        try {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            code = httpResponse.getStatusLine().getStatusCode();
            if (code == 200) {

                String jsonResult = EntityUtils.toString(httpResponse.getEntity());
                map = com.ocom.common.utils.GsonBuilder.build().fromJson(jsonResult, map.getClass());
            }

        } catch (Exception e) {
            map.put("Code", code);
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (Exception e) {
            }
        }
        return map;
    }

    public static void main(String[] args) {
//        String par="{\"Uid\": \"1341C1\",\"TypeId\": \"1\"}";//getwallet
//        String par="{\"Uid\": \"1341C1\",\"Channel\": \"1\"}";//getUser
//        String par="{\"OrderNumber\": \"2000222001111107\",\"PaymentStatus\": 1,\"PaymentNumber\": \"ZK2000222001111107\",\"Describe\": \"1\",\"TimeStamp\": " + new Date().getTime() + "}";//paymentCallBack
        String par="{\"MachineId\": \"111\",\"OpenTime\": \"1651027392618\",\"PosType\": \"1\",\"PosMoney\": \"500\",\"PosAddress\": \"北斗产业园\",\"OrderNumber\": \"2000222001111112\",\"Uid\": \"1341C1\",\"ReceiveTime\": \"1651027392618\",\"Describe\": \"描述\",\"TimeStamp\": "+ new Date().getTime() +"}";
//        String par="{\"UserName\": \"ps\",\"UserPassword\": \"t2exf9q3qun3rcj1\",\"TimeStamp\": " + new Date().getTime() + "}";//getToken
        String sid="170000000000051001";
        String aseSecret= "Ibn2XWYs7MfNoU8RtVjfNtWswizAaA5p";
        String md5Secret="uww40z1C";
        SecretRule secretRule=new SecretRule().setSid(sid).setAes(aseSecret).setMd5(md5Secret);
        /*aes加密*/
        String aesData = SecretUtil.encryptByAES(secretRule, par);
        /*aes加密后再md5加密*/
        System.out.println("加密前参数data:"+par);
        String md5Data = SecretUtil.encryptByMD5(secretRule, aesData);
        System.out.println("X-Sign:"+md5Data+",参数data:"+aesData);
    }

}
